package com.zmm.type.controller;

import com.zmm.common.base.model.Result;
import com.zmm.common.base.model.ResultCode;
import com.zmm.common.constant.NumberConstant;
import com.zmm.common.enums.CategoryEnum;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.common.pre.handle.Assert;
import com.zmm.type.model.Category;
import com.zmm.type.service.CategoryService;
import com.zmm.type.vo.CategoryTreeNode;
import com.zmm.type.vo.CategoryVo;
import com.zmm.util.convert.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Name CategoryController
 * @Author Zebulon
 * @Date By 2021-06-27 18:59:07
 */
@Slf4j
@Controller
@RequestMapping("/category")
public class CategoryController {

	@Resource
	private CategoryService categoryService;



	/**
	 * 控制器 - 分类管理 - 查询分类全部信息
	 * @author: 900045
	 * @date: 2021-06-28 16:37:49
	 * @throws 
	 * @param categoryType: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/all/list", method = RequestMethod.GET)
	public Result queryAllCategory(Integer categoryType) {
		List<CategoryVo> cateList = categoryService.queryAllCategory(categoryType);
		return Result.ok().setData(cateList);

	}
	
	/**
	 * 新增分类
	 * @author: 900045
	 * @date: 2021-07-05 16:12:14
	 * @throws 
	 * @param treeNode: 
	 * @param categoryType: 
	 * @param sort: 
	 * @param categoryImgId: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public Result addCategory(CategoryTreeNode treeNode, Integer categoryType, Integer sort, Long categoryImgId){
		if (ObjectUtils.isEmpty(treeNode)) {
			return Result.error(100003,ResultCode.PARAMETER_ERROR.getDesc());
		}
		Category category ;
		
		try {
			if (treeNode.getCategoryId() == 0) {
				//保存
				category = cateFromTreeNode(treeNode, categoryType);
				//查询同一父类目下是否存在同名类目名
				if (categoryService.isExistName(category)) {
					// result.setError("同级类目下存在相同的类目名称")
					return Result.error(100004,ResultCode.RECORD_ALREADY_EXISTS.getDesc());
				}
				
				if (CategoryEnum.FRONT_CATE.getCode().equals(categoryType)) {
					if (sort != 0) {
						category.setSort(sort);
					}
					if (categoryImgId != 0) {
						category.setCategoryImgId(categoryImgId);
					}
				}
				// 新增
				categoryService.addCategory(category);
			} else {
				// 修改
				return modifyCategory(treeNode,categoryType,sort,categoryImgId);
			}
		}catch (Exception e) {
			log.error("SAVE-CATEGORY  end: e-{}", e);
		}
		return Result.ok();
	}

	/**
	 * 修改分类
	 * @author: 900045
	 * @date: 2021-07-05 16:11:37
	 * @throws 
	 * @param treeNode: 
	 * @param categoryType: 
	 * @param sort: 
	 * @param categoryImgId: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/modify",method = RequestMethod.POST)
	public Result modifyCategory(CategoryTreeNode treeNode, Integer categoryType, Integer sort, Long categoryImgId){
		
		if (treeNode.getCategoryId() == 0 && StringUtil.isBlank(treeNode.getCategoryName())) {
			return Result.error(100003,ResultCode.PARAMETER_ERROR.getDesc());
		}
		Category category = null;
		
		try{
			category = categoryService.findCategoryByParentId(treeNode.getCategoryId());
			if (category != null) {
				category.setCategoryName(treeNode.getCategoryName());
			}
			if (CategoryEnum.FRONT_CATE.getCode().equals(categoryType)) {
				category.setSort(sort);
				category.setCategoryImgId(categoryImgId);
			}
			//判断修改的类目名是否重名
			if (categoryService.isExistName(category)){
				// result.setError("同级类目下存在相同的类目名称,请重新命名")
				return Result.error(100004,ResultCode.RECORD_ALREADY_EXISTS.getDesc());
			}
			categoryService.modifyCategory(category);
		}catch (Exception e) {
			log.error("SAVE-CATEGORY {} end: e-{}", category != null ? category.getCategoryName() : null, e);
		}
		return Result.ok();
	}

	/**
	 * 根据 categoryId 删除分类
	 * @author: 900045
	 * @date: 2021-07-05 16:32:25
	 * @throws 
	 * @param categoryId: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/remove/categoryId",method = RequestMethod.POST)
	public Result removeByCategoryId(Integer categoryId) {
		if (categoryId == null || categoryId == 0) {
			return Result.error(100003,ResultCode.PARAMETER_ERROR.getDesc());
		}
		Integer remove = 0;
		try {
			remove = categoryService.removeCategory(categoryId);
			if (remove <= 0) {
				return Result.error(100005,ResultCode.FIELD.getDesc());
			}
		} catch (Exception e) {
			log.error("CATEGORY-DELETE {} : end {}", categoryId, e);
		}
		return Result.ok();
	}

	/**
	 * 查询所有的类目节点
	 * @author: 900045
	 * @date: 2021-07-05 16:49:09
	 * @throws 
	 * @param categoryType: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/tree",method = RequestMethod.GET)
	public Result findCategoryTree(Integer categoryType) {
		Assert.isPositiveInteger(categoryType);
		List<CategoryTreeNode> treeList = categoryService.findCategoryTree(categoryType);
		return Result.ok().setData(treeList);
	}


	/**
	 * 根据类目categoryId 显示类目详情
	 * @author: 900045
	 * @date: 2021-07-05 17:05:26
	 * @throws 
	 * @param categoryId: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/find/info",method = RequestMethod.GET)
	public Result findCategoryByCategoryId(Integer categoryId) {
		if (categoryId == 0) {
			return Result.error(100003,ResultCode.PARAMETER_ERROR.getDesc());
		}
		Category category = null;
		try {
			category = categoryService.queryCategoryById(categoryId);
		} catch (Exception e) {
			log.error("cate-findCategoryById {} error: {}", categoryId, e);
		}
		return Result.ok().setData(category);
	}


	/**
	 * 二级类目对应的所有的三级类目节点
	 * @author: 900045
	 * @date: 2021-07-05 17:12:22
	 * @throws 
	 * @param category: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@RequestMapping(value = "/find/three/category",method = RequestMethod.GET)
	public Result findThreeCategoryBySecondCategoryId(Category category) {
		if (ObjectUtils.isEmpty(category)) {
			return Result.error(100003,ResultCode.PARAMETER_ERROR.getDesc());
		}
		List<Category> categoryList = null;
		try {
			categoryList = categoryService.findThreeCategoryBySecondCategoryId(category);
		} catch (Exception e) {
			log.error("cate-findCategoryById {} error: {}", category.getSecondCategoryId(), e);
		}
		return Result.ok().setData(categoryList);
	}
	
	
	

	private Category cateFromTreeNode(CategoryTreeNode treeNode, Integer categoryType) throws BusinessException {
		//要保存的类目
		Category category = new Category();
		if (treeNode == null) {
			return null;
		}
		String categoryName = treeNode.getCategoryName();
		if (StringUtil.isBlank(categoryName)) {
			throw new BusinessException("类目名不能为空");
		}
		if (categoryType == 0) {
			throw new BusinessException("类目类型不能为空");
		}

		//上级类目
		Category parentCate = null;
		Integer categoryId = treeNode.getParentId();
		if (categoryId != 0) {
			//上级类目对象 
			parentCate = categoryService.findCategoryByParentId(treeNode.getParentId());
		}
		category.setCategoryType(categoryType);
		category.setCategoryName(categoryName);
		//设置类目各级分类
		if (NumberConstant.ONE.equals(treeNode.getLevel())) {
			//如果父类目则新增的是二级类
			category.setFirstCategoryId(treeNode.getParentId());
			category.setFirstCategoryName(parentCate.getCategoryName());
		} else {
			if (NumberConstant.TWO.equals(treeNode.getLevel())) {
				//如果父类目则新增的是三级类
				category.setSecondCategoryId(treeNode.getParentId());
				category.setFirstCategoryId(parentCate.getFirstCategoryId());
				category.setFirstCategoryName(parentCate.getFirstCategoryName());
				category.setSecondCategoryName(parentCate.getCategoryName());
			} else {
				//新增的是顶级类目
				category.setFirstCategoryId(0);
			}
		}
		return category;
	}
}
